<template>
  <div>
    <el-dialog
      v-model="_dialogVisible"
      :title="`${dept_id ? '修改' : '添加'}部门`"
      :before-close="handleClose"
      destroy-on-close
      draggable
    >
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="80">
        <el-form-item label="部门类型" prop="dept_type">
          <el-radio-group v-model="formData.dept_type">
            <el-radio-button :label="1">公司</el-radio-button>
            <el-radio-button :label="2">中心</el-radio-button>
            <el-radio-button :label="3">部门</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级部门" prop="parent_id">
          <el-tree-select
            v-model="formData.parent_id"
            :data="treeData"
            :props="{ label: 'dept_name', value: 'dept_id' }"
            check-strictly
            placeholder="请选择上级部门"
          />
        </el-form-item>
        <el-form-item label="部门名称" prop="dept_name">
          <el-input v-model="formData.dept_name" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门状态" prop="dept_status">
          <el-radio-group v-model="formData.dept_status">
            <el-radio-button :label="1">正常</el-radio-button>
            <el-radio-button :label="2">停用</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="handleSubmit">
          {{ dept_id ? '修改' : '添加' }}
        </el-button>
        <el-button @click="handleClose">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, watch, onMounted } from 'vue'
import { DeptParam, VoListItem } from '@/api/auth/model/deptModel'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useForm } from '@/hooks'
import { deptShow, store, update, getDeptVoList } from '@/api/auth/dept'
import handleTree from '@/utils/tree'

const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false,
  },
  dept_id: {
    type: Number,
    default: 0,
  },
})

const { validate, resetForm } = useForm()
const emit = defineEmits(['update:dialogVisible', 'success'])
const formRef = ref<FormInstance>(null!)

const _dialogVisible = ref<boolean>(false)

let treeData = ref<VoListItem[]>([])

let formData = ref<DeptParam | any>({
  dept_id: 0,
  parent_id: 0,
  dept_type: 1,
  dept_name: '',
  dept_status: 1,
})

watch(
  () => props.dialogVisible,
  () => {
    props.dialogVisible && handVoList()
    _dialogVisible.value = props.dialogVisible
  }
)

watch(
  () => _dialogVisible.value,
  () => {
    emit('update:dialogVisible', _dialogVisible.value)
  }
)

//获取数据
const detail = async () => {
  if (props.dept_id) {
    const { code, message, data } = await deptShow({ id: props.dept_id })
    if (code != 200) {
      ElMessage.error(message)
      return
    }
    formData.value = data
  }
}

watch(
  () => [props.dept_id, props.dialogVisible],
  () => {
    props.dialogVisible && props.dept_id && detail()
  }
)

const handVoList = async () => {
  const { code, message, data } = await getDeptVoList()
  if (code != 200) {
    ElMessage.error(message)
    return
  }
  treeData.value = handleTree.handleTree(
    data,
    'dept_id',
    'parent_id',
    'children'
  )
  treeData.value?.unshift({ dept_id: 0, parent_id: 0, dept_name: '无' })
}

const handleClose = () => {
  resetForm(formRef.value)
  _dialogVisible.value = false
}

const rules = reactive<FormRules<DeptParam>>({
  dept_type: [
    {
      required: true,
      message: '请选择部门类型',
      trigger: 'blur',
    },
  ],
  parent_id: [
    {
      required: true,
      message: '请选择上级部门',
      trigger: 'blur',
    },
  ],
  dept_name: [
    {
      required: true,
      message: '请输入部门名称',
      trigger: 'blur',
    },
  ],
  dept_status: [
    {
      required: true,
      message: '请选择部门状态',
      trigger: 'blur',
    },
  ],
})

const handleSubmit = async () => {
  await validate(formRef.value)
  //发送请求
  let result
  if (!props.dept_id) {
    result = await store(formData.value)
  } else {
    result = await update(formData.value)
  }
  const { code, message } = result
  if (code != 200) {
    ElMessage.error(`${props.dept_id ? '修改' : '添加'}${message}`)
    return
  }
  handleClose()
  ElMessage.success(`${props.dept_id ? '修改' : '添加'}${message}`)
  emit('success')
}

onMounted(() => {})
</script>

<style scoped></style>
